﻿Imports Entities
Public Class VentaDAL
    Public Shared Function Create(ByVal zObject As VENTAS) As VENTAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                zObject.ACTIVO = True
                bd.VENTAS.Add(zObject)
                bd.SaveChanges()
                Return zObject
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As VENTAS) As VENTAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                              Where rec.VENTA = zObject.VENTA
                              Select rec).Single

                query.RUT = zObject.RUT
                query.PATENTE = zObject.PATENTE
                query.FECHA = zObject.FECHA
                query.PRECIOTOTAL = zObject.PRECIOTOTAL
                query.KILOS = zObject.KILOS
                query.SUBTOTAL = zObject.SUBTOTAL
                query.DESCTO = zObject.DESCTO
                query.DESCTOPOS = zObject.DESCTOPOS
                query.FECHAPAGO = zObject.FECHAPAGO
                query.PEDIDOPOSID = zObject.PEDIDOPOSID
                query.PENDIENTE = zObject.PENDIENTE
                query.GARANTIA = zObject.GARANTIA
                query.CLIENTE = zObject.CLIENTE

                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function ListByFechas(ByVal zFechaDesde As Date, ByVal zFechaHasta As Date) As List(Of VENTAS)
        Dim fDesde As Date = CDate(zFechaDesde.ToShortDateString + " 00:00:00.000")
        Dim fHasta As Date = CDate(zFechaHasta.ToShortDateString + " 23:59:59.999")
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.ventas
                            Where rec.FECHA >= (fDesde) And rec.FECHA <= (fHasta) And rec.ACTIVO And rec.DISTRIBUIDOR = 0
                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function ListBalonPendientes() As List(Of VENTAS)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.ventas
                            Where Not rec.GARANTIA.Equals(Nothing) And rec.GARANTIA > 0 And rec.ACTIVO
                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function ListToCierreDiario(ByVal zFecha As Date, ByVal zCamion As String, ByVal zChofer As String) As List(Of VENTAS)
        Dim fDesde As Date = CDate(zFecha.ToShortDateString + " 00:00:00.000")
        Dim fHasta As Date = CDate(zFecha.ToShortDateString + " 23:59:59.999")
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.VENTAS
                            Where rec.FECHA >= (fDesde) And rec.FECHA <= fHasta And
                            rec.RUT = zChofer And rec.PATENTE = zCamion And rec.ACTIVO
                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function ListByCLienteId(ByVal zCLienteId As Long) As List(Of VENTAS)

        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.VENTAS
                            Where rec.CLIENTE = zCLienteId And rec.ACTIVO
                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function ListByBoletaId(ByVal zBoletaId As Long) As List(Of VENTAS)

        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.VENTAS
                            Where rec.BOLETA = zBoletaId And rec.ACTIVO
                            Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function List() As List(Of VENTAS)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                            Where rec.ACTIVO
                            Select rec).ToList
                Return query
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function ListByPedidoPOSId() As List(Of VENTAS)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.ventas
                            Where Not rec.PEDIDOPOSID.Equals(Nothing) And rec.PEDIDOPOSID > 0 And Not rec.DEVUELTO And rec.ACTIVO
                            Select rec).ToList
                Return query
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function UpdateDevuelto(ByVal zVentaId As Long) As VENTAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                            Where rec.VENTA = zVentaId
                            Select rec).SingleOrDefault
                query.Devuelto = True
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Existe(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                            Where rec.VENTA = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function SearchById(ByVal zId As Integer) As VENTAS
        Using bd As New gas_gasdesuvecinoEntities
            Return bd.VENTAS.Find(zId)
        End Using
    End Function
    Public Shared Function UpdateGarantiaPendiente(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                          Where rec.VENTA = zId
                          Select rec).SingleOrDefault

                query.GARANTIA = Nothing
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function SearchByBoletaId(ByVal zId As Long) As VENTAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VENTAS
                          Where rec.BOLETA = zId).SingleOrDefault
                Return query
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zVentaId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim del = (From rec In bd.VENTAS
                          Where rec.VENTA = zVentaId
                          Select rec).Single

                del.ACTIVO = False
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function ListPendientes() As List(Of ventas)
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.ventas
                          Where rec.PENDIENTE > 0
                          Select rec

                Return query.ToList
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
